package icu.d4peng.basic.http.feign;

import java.util.Map;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author d4peng d4peng@qq.com
 * @date 2023-11-26 15:36:16
 */

class FeignClientMetricsEnabledCondition implements Condition {

	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		FeignClientProperties feignClientProperties = context.getBeanFactory()
			.getBeanProvider(FeignClientProperties.class)
			.getIfAvailable();
		if (feignClientProperties != null) {
			Map<String, FeignClientProperties.FeignClientConfiguration> feignClientConfigMap = feignClientProperties
				.getConfig();
			if (feignClientConfigMap != null) {
				FeignClientProperties.FeignClientConfiguration feignClientConfig = feignClientConfigMap
					.get(context.getEnvironment().getProperty("roc.feign.client.name"));
				if (feignClientConfig != null) {
					FeignClientProperties.MetricsProperties metrics = feignClientConfig.getMetrics();
					if (metrics != null && metrics.getEnabled() != null) {
						return metrics.getEnabled();
					}
				}
			}
		}

		return true;
	}

}
